﻿using GN.Pay.Application.MerchantApp.Dto.Input;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace GN.Pay.UnitTest.MerchantAppTest
{
    public class MerchantAppChannelManager
    {

        /// <summary>
        /// 创建通道
        /// </summary>
        /// <param name="channelId"></param>
        /// <param name="channelName"></param>
        /// <param name="routeName"></param>
        /// <param name="tradeNumberSuffix"></param>
        /// <returns></returns>
        private MerchantAppChannelRegisterInput CreateTradeChannel(string appId, string channelId)
        {
            var channel = new MerchantAppChannelRegisterInput()
            {
                AppId = appId,
                ChannelId = channelId,
                ConfigureInfo = new Dictionary<string, object>()
            };
            return channel;
        }

        private string StreamBase64(string fileName)
        {
            using (var fs = new FileStream(fileName,
                FileMode.Open,
                FileAccess.Read,
                FileShare.Read))
            {
                byte[] bytes = new byte[1024];
                int count = 0;
                using (MemoryStream ms = new MemoryStream())
                {
                    while ((count = fs.Read(bytes, 0, bytes.Length)) > 0)
                    {
                        ms.Write(bytes, 0, count);
                    }
                    return Convert.ToBase64String(ms.ToArray());
                }
            }
        }

        private MerchantAppChannelRegisterInput AliPay(MerchantAppChannelRegisterInput input)
        {
            if (input.AppId.Equals("gn77822533557191132703", StringComparison.InvariantCultureIgnoreCase))
            {
                //下行
                input.ConfigureInfo["AppId"] = "2017040706583188";
                input.ConfigureInfo["AccountId"] = "2088621785432459";
                input.ConfigureInfo["Key"] = "5apacclg48mgeoui6amz0rdheah9zl18";
                input.ConfigureInfo["PrivateKey"] = "MIICXgIBAAKBgQDF9AElKe/QANAOSPGFJiaBgMhk3NTMhoNNHWSCoIA2bUa7BqxPjUg0hU0pzVdRtQlPjl7rnBT+1sc8YOavc6Q1fSSCfZgrcFqxOy+5IkG6HQvZUcMzLRbygygFVLu0+BIz0658gW0Wmx6Zf0NXB1SM2nc9oskCLy08B754SKB3zwIDAQABAoGAck9eXeyirFXbz3P17HLEZ6JLllsjk1vV9LEVeo1enI9r+dJ3rJSkbpfPoGUQmIFXeBZ538rJIQQ8FzTJ4eWAm9Gs5D3VTyc0c2dwnmlUPzUDJi/kebScMNa0BH3fEbNVFV4wQBKWqrl0yQtp0F5t+CVyXTNh1k8+xrOQ4OuHYbkCQQD2qyizHkJcZxsFlMn7g24PuJMM+7jtkciN2/0PQyeDGzh13Zd2Rn1axDvISw/Mv1+4aWKqxZCaLhCp5Q70eiRbAkEAzXEQm75oPu27PRfhjGUSEMlJE1xMPGHWRPL9obT5stqiWfg4qqT0xfljRFjzQqFOy/riLLVIYRl003ce9TBEnQJBAKM2tYNvbjF9959FyDAn0teMJaDdnotzfuR+v38tzCMwC7fRm7qjAabhWT06jYXIyylWcfbVKs0Z1eTUr4SMnSkCQQCD9DFEt6HheXSgIuyd7nUSDxc+tjPJPNYRZzl9We5f8IoC26oaAOWvGyWlSrTEgTNJVRVcvfuFUoJ1KLfbqGsJAkEAyDpbnjhbGSGKzjRRvPFB4e/RoxQBo+hxcNOvgg5sdcNzst9qj6Q5uKbugtGOdCMQCIp/Jf84MZZ8Vd+z18fT4Q==";
                input.ConfigureInfo["PublicKey"] = "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCnxj/9qwVfgoUh/y2W89L6BkRAFljhNhgPdyPuBV64bfQNN1PjbCzkIM6qRdKBoLPXmKKMiFYnkd6rAoprih3/PrQEB/VsW8OoM8fxn67UDYuyBTqA23MML9q1+ilIZwBC2AQ2UBVOrFXfFl75p6/B5KsiNG9zpgmLCUYuLkxpLQIDAQAB";
                input.ConfigureInfo["SellerEmail"] = "gnw@guinong360.com";
            }
            else
            {
                //上行
                input.ConfigureInfo["AppId"] = "2016060401480481";
                input.ConfigureInfo["AccountId"] = "2088121828701307";
                input.ConfigureInfo["Key"] = "ibh87vayids361ob47pmufbxswnstpcf";
                input.ConfigureInfo["PrivateKey"] = "MIICXAIBAAKBgQC9cf+diM7Q+64eYTSUbAJAm/I6/co8Y1w33+S6AOVCWBYZzYMXeCK7YXRf5F3kiuBqedXP0tL3Nl5YyHDk+0QOH6shqyp1l4RLRBhtmaclRMuFl0te81UQIDNZO5KImQtQsFu4DraNeGvleteb7cIw+pMQMgKEr0DhlDi+nh1QQQIDAQABAoGAPaEATvdakJHPqwJkYIRilzIkMxys1chdD7A+cGQqMNMHHY3Fv7TNV7f0a92Wzep/5yOLcvhauSFuv+M1FsjG8mICKHz6BAR4bLD+059c+U1boEbrMwuMknekGOnIj2LRZA2b45FwwUwHQxzYVvACJaY1RHUM5S3GkfSbNOMmd6ECQQDcdYWsKENvx+pvRr+/NNxSZwTNcgnBZFPdxO3rF2C9giPwHns//UHH4seOjQ1SDQCR0Bxy2C5+NcTowsFMerVDAkEA2/yFDFSzFgHpaWG2tmqwLvKCGk422uohjWAZfyYITy7LvgfQY0ba9Ce46iLvViW0GnAFM6uCuzqRofwq3v7KKwJAUA3E6oz8X/ziXmH8gvw4/Rs3nTrJ3BYIdkFqe7HTiT1Wu3z2GHhoiKMK//KsYZ0jFc/SZMmnqGDyDeuasaDlbQJAMbE1DLgFTC9aXmeoMkSfT7uQEAki8wh4U8sld0dMETbs5xw0wng69XQTnTAPCFK0AFTVl/piKZDtXL3toTnEyQJBAJXwpLFII3CLv7e06QK9puqqH6PSfXPGZU/xhii4WfbKVVX82hEPv1CuTv7ArwYto1/Tpm5onSdjUMa1B8BgAMs=";
                input.ConfigureInfo["PublicKey"] = "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCnxj/9qwVfgoUh/y2W89L6BkRAFljhNhgPdyPuBV64bfQNN1PjbCzkIM6qRdKBoLPXmKKMiFYnkd6rAoprih3/PrQEB/VsW8OoM8fxn67UDYuyBTqA23MML9q1+ilIZwBC2AQ2UBVOrFXfFl75p6/B5KsiNG9zpgmLCUYuLkxpLQIDAQAB";
                input.ConfigureInfo["SellerEmail"] = "gnw@guinong360.com";
            }
            input.ConfigureInfo["IsSupportRefund"] = true;
            return input;
        }

        /// <summary>
        /// 即时到账
        /// </summary>
        /// <returns></returns>
        public MerchantAppChannelRegisterInput AliPay_Transfer(string appId)
        {
            var input = CreateTradeChannel(appId, "AliPay.Transfer");
            return AliPay(input);
        }

        /// <summary>
        /// 即时到账
        /// </summary>
        /// <returns></returns>
        public MerchantAppChannelRegisterInput AliPay_MoblieWap(string appId)
        {
            var input = CreateTradeChannel(appId, "AliPay.MoblieWap");
            var pay = AliPay(input);
            input.ConfigureInfo["PublicKey"] = "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDDI6d306Q8fIfCOaTXyiUeJHkrIvYISRcc73s3vF1ZT7XN8RNPwJxo8pWaJMmvyTn9N4HQ632qJBVHf8sxHi/fEsraprwCtzvzQETrNRwVxLO5jVmRGi60j8Ue1efIlzPXV9je9mkjzOmdssymZkh2QhUrCmZYI/FCEa3/cNMW0QIDAQAB";
            return pay;
        }

        /// <summary>
        /// 手机App
        /// </summary>
        /// <returns></returns>
        public MerchantAppChannelRegisterInput AliPay_App(string appId)
        {
            var input = CreateTradeChannel(appId, "AliPay.App");

            return AliPay(input);
        }


        /// <summary>
        /// 微信扫码
        /// </summary>
        /// <returns></returns>
        public MerchantAppChannelRegisterInput WeiXin_QRCode(string appId)
        {
            var input = CreateTradeChannel(appId, "WeiXin.QRCode");
            if (input.AppId.Equals("gn77822533557191132703", StringComparison.InvariantCultureIgnoreCase))
            {
                //下行
                input.ConfigureInfo["AccountId"] = "1461485002";
                input.ConfigureInfo["AppId"] = "wxcc1b0fa7864aaa2b";
                input.ConfigureInfo["Key"] = "c03a25364e884053ae0f38e1ed187034";
                input.ConfigureInfo["RefundCretRawBase64"] = StreamBase64(@"E:\Api 配置\微信\down\QrCode\apiclient_cert.p12");
                input.ConfigureInfo["IsSupportRefund"] = true;
            }
            else
            {
                input.ConfigureInfo["AccountId"] = "1308330901";
                input.ConfigureInfo["AppId"] = "wxd0dac157ad4151fd";
                input.ConfigureInfo["Key"] = "FB2010F2760F46AEB72AC7B8CE63217A";
                input.ConfigureInfo["RefundCretRawBase64"] = StreamBase64(@"E:\Api 配置\微信\QrCode\apiclient_cert.p12");
                input.ConfigureInfo["IsSupportRefund"] = true;
            }

            return input;
        }

        /// <summary>
        /// 微信H5支付配置
        /// </summary>
        /// <returns></returns>
        public MerchantAppChannelRegisterInput WeiXin_H5Pay(string appId)
        {
            var input = CreateTradeChannel(appId, "WeiXin.MWEB");
            if (input.AppId.Equals("gn77822533557191132703", StringComparison.InvariantCultureIgnoreCase))
            {
                //下行
                input.ConfigureInfo["AccountId"] = "1461485002";
                input.ConfigureInfo["AppId"] = "wxcc1b0fa7864aaa2b";
                input.ConfigureInfo["Key"] = "c03a25364e884053ae0f38e1ed187034";
                input.ConfigureInfo["RefundCretRawBase64"] = StreamBase64(@"E:\Api 配置\微信\down\QrCode\apiclient_cert.p12");
                input.ConfigureInfo["IsSupportRefund"] = true;
            }
            else
            {
                input.ConfigureInfo["AccountId"] = "1308330901";
                input.ConfigureInfo["AppId"] = "wxd0dac157ad4151fd";
                input.ConfigureInfo["Key"] = "FB2010F2760F46AEB72AC7B8CE63217A";
                input.ConfigureInfo["RefundCretRawBase64"] = StreamBase64(@"E:\Api 配置\微信\QrCode\apiclient_cert.p12");
                input.ConfigureInfo["IsSupportRefund"] = true;
            }

            return input;
        }


        /// <summary>
        /// 微信JSAPI
        /// </summary>
        /// <returns></returns>
        public MerchantAppChannelRegisterInput WeiXin_JsApi(string appId)
        {
            var input = CreateTradeChannel(appId, "WeiXin.JSAPI");
            if (input.AppId.Equals("gn77822533557191132703", StringComparison.InvariantCultureIgnoreCase))
            {
                //下行
                input.ConfigureInfo["AccountId"] = "1461485002";
                input.ConfigureInfo["AppId"] = "wxcc1b0fa7864aaa2b";
                input.ConfigureInfo["Key"] = "c03a25364e884053ae0f38e1ed187034";
                input.ConfigureInfo["RefundCretRawBase64"] = StreamBase64(@"E:\Api 配置\微信\down\QrCode\apiclient_cert.p12");
                input.ConfigureInfo["IsSupportRefund"] = true;
            }
            else
            {
                input.ConfigureInfo["AccountId"] = "1308330901";
                input.ConfigureInfo["AppId"] = "wxd0dac157ad4151fd";
                input.ConfigureInfo["Key"] = "FB2010F2760F46AEB72AC7B8CE63217A";
                input.ConfigureInfo["RefundCretRawBase64"] = StreamBase64(@"E:\Api 配置\微信\QrCode\apiclient_cert.p12");
                input.ConfigureInfo["IsSupportRefund"] = true;
            }
            return input;
        }

        public MerchantAppChannelRegisterInput WeiXin_App(string appId)
        {
            var input = CreateTradeChannel(appId, "WeiXin.App");
            if (input.AppId.Equals("gn77822533557191132703", StringComparison.InvariantCultureIgnoreCase))
            {
                //下行
                input.ConfigureInfo["AccountId"] = "1471482202";
                input.ConfigureInfo["AppId"] = "wxc2123433ae714376";
                input.ConfigureInfo["Key"] = "d8f385bc45484502992fb61130d03e36";
                input.ConfigureInfo["RefundCretRawBase64"] = StreamBase64(@"E:\Api 配置\微信\down\app\apiclient_cert.p12");
                input.ConfigureInfo["IsSupportRefund"] = true;
            }
            else
            {
                input.ConfigureInfo["AccountId"] = "1480178632";
                input.ConfigureInfo["AppId"] = "wx5e3f5b903e71e540";
                input.ConfigureInfo["Key"] = "1E15F67DED5E9C39B897F5E1AA24697D";
                input.ConfigureInfo["RefundCretRawBase64"] = StreamBase64(@"E:\Api 配置\微信\App\apiclient_cert.p12");
                input.ConfigureInfo["IsSupportRefund"] = true;
            }
            return input;
        }

        private void Load_GyCCB(MerchantAppChannelRegisterInput input)
        {
            input.ConfigureInfo["AccountId"] = "168000000010";
            input.ConfigureInfo["SubMercName"] = "贵州贵农网络科技有限公司";
            input.ConfigureInfo["SignPfxRawBase64"] = StreamBase64(@"E:\Api 配置\贵阳银行\GuiNong.Plugin.Payment.GYBank\Cert\bank.pfx");
            input.ConfigureInfo["SignPfxPassWord"] = "guinong";
            input.ConfigureInfo["MasterID"] = "2002375598";
            input.ConfigureInfo["SignVerifyCerRawBase64"] = StreamBase64(@"E:\Api 配置\贵阳银行\GuiNong.Plugin.Payment.GYBank\Cert\payget.cer");
            input.ConfigureInfo["IsSupportRefund"] = true;

            //input.ConfigureInfo["AccountId"] = "168000000001";
            //input.ConfigureInfo["SubMercName"] = "百度外卖";
            //input.ConfigureInfo["SignPfxRawBase64"] = StreamBase64(@"F:\GuiNong4.0\GN.Pay\doc\贵阳银行快捷支付开发包\商户测试\testbank.pfx");
            //input.ConfigureInfo["SignPfxPassWord"] = "111111";
            //input.ConfigureInfo["MasterID"] = "2002055243";
            //input.ConfigureInfo["SignVerifyCerRawBase64"] = StreamBase64(@"F:\GuiNong4.0\GN.Pay\doc\贵阳银行快捷支付开发包\商户测试\paygate_cert.cer");
            //input.ConfigureInfo["IsSupportRefund"] = true;
        }

        /// <summary>
        /// 贵阳银行Web
        /// </summary>
        /// <returns></returns>
        public MerchantAppChannelRegisterInput GyCCB_WebPay(string appId)
        {
            var input = CreateTradeChannel(appId, "Gyb.WebPay");

            Load_GyCCB(input);

            return input;
        }

        /// <summary>
        /// 贵阳银行协议支付
        /// </summary>
        /// <returns></returns>
        public MerchantAppChannelRegisterInput GyCCB_ProtocolPay(string appId)
        {
            var input = CreateTradeChannel(appId, "Gyb.ProtocolPay");


            Load_GyCCB(input);

            return input;
        }

        public MerchantAppChannelRegisterInput UnionPay(string appId)
        {
            var input = CreateTradeChannel(appId, "Union.WebPay");
            input.ConfigureInfo["AccountId"] = "898520148160237";
            input.ConfigureInfo["SignPfxRawBase64"] = StreamBase64(@"E:\Api 配置\银联\Cert\898520148160237.pfx");
            input.ConfigureInfo["SignPfxPassWord"] = "201608";
            input.ConfigureInfo["SignVerifyCerRawBase64"] = StreamBase64(@"E:\Api 配置\银联\Cert\acp_prod_verify_sign.cer");
            input.ConfigureInfo["IsSupportRefund"] = true;

            return input;
        }

        public MerchantAppChannelRegisterInput YJPay(string appId)
        {
            var input = CreateTradeChannel(appId, "YJPay.WebPay");
            input.ConfigureInfo["AccountId"] = "20161230020012250624";
            input.ConfigureInfo["Key"] = "f14c1983a09c0df574b1c31ff9e8b936";
            input.ConfigureInfo["IsSupportRefund"] = true;
            return input;
        }

        public List<MerchantAppChannelRegisterInput> RegisterInputList(string appId)
        {
            List<MerchantAppChannelRegisterInput> items = new List<MerchantAppChannelRegisterInput>();
            MerchantAppChannelRegisterInput input;

            //input = AliPay_Transfer(appId);
            //items.Add(input);

            //input = AliPay_MoblieWap(appId);
            //items.Add(input);

            //input = AliPay_App(appId);
            //items.Add(input);

            //input = WeiXin_QRCode(appId);
            //items.Add(input);

            //input = WeiXin_App(appId);
            //items.Add(input);

            //input = WeiXin_JsApi(appId);
            //items.Add(input);

            input = WeiXin_H5Pay(appId);
            items.Add(input);

            //input = GyCCB_WebPay(appId);
            //items.Add(input);

            //input = GyCCB_ProtocolPay(appId);
            //items.Add(input);

            //input = UnionPay(appId);
            //items.Add(input);

            //input = YJPay(appId);
            //items.Add(input);

            return items;
        }
    }
}
